<?


class classEventLog {
	private $parent;

	public function __construct(&$parent) { $this->parent = $parent;}

	public function add($params=array()) {
		$fields = array();
		$values = array();
		if (is_array($params) || is_object($params)) {
			foreach	($params AS $name=>$value) {
				switch ($name) {
					case 'device_id':
						$fields[] = $name;
						$values[] = (int)$value;
						break;
					case 'description':
					case 'notes':
						$fields[] = $name;
						$values[] = "'".$this->parent->db->escape($value)."'";
						break;
					case 'status':
						switch ($value) {
							case 'log':
							case 'open':
							case 'resolved':
								$fields[] = $name;
								$values[] = "'".$this->parent->db->escape($value)."'";
								break;
						}
						break;
					case 'type':
						switch ($value) {
							case 'system':
							case 'debug':
							case 'change':
							case 'alert':
								$fields[] = $name;
								$values[] = "'".$this->parent->db->escape($value)."'";
								break;
						}
						break;
					case 'level':
						switch ($value) {
							case 'info':
							case 'notification':
							case 'warning':
							case 'critical':
								$fields[] = $name;
								$values[] = "'".$this->parent->db->escape($value)."'";
								break;
						}
						break;
				}
			}		
		}
		
		if (!empty($fields) && !empty($values)) {
			if (!in_array('level',$values)) { $fields[] = 'level'; $values[] = "'info'";}
			if (!in_array('type',$values)) { $fields[] = 'type'; $values[] = "'system'";}		
			if (!in_array('status',$values)) { $fields[] = 'status'; $values[] = "'log'";}
			$sql = "INSERT INTO event_log (".implode(',',$fields).") VALUES (".implode(',',$values).");";
			if ($this->parent->db->query($sql)) {
				return true;
			} else {
				error_log('ERROR: '.$this->parent->db->lastError);
				return false;
			}
		} else {
			return false;
		}
	}
}